﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Genetic;

namespace Genetic
{
    public class TetrisChromosome : PermutationChromosome
    {
        public TetrisChromosome(int g_nr):base(g_nr)
        {
            this.maxValue = 255;
            Generate();
        }
        public TetrisChromosome(TetrisChromosome ch)
            : base(ch)
        {}
      
        public override void Generate()
        {
        
			// create ascending permutation initially
			for ( int i = 0; i < length; i++ )
			{
                val[i] =(ushort)rand.Next(0,maxValue);
			}
			

        }
        public override void Crossover(IChromosome pair)
        {
            TetrisChromosome p = (TetrisChromosome)pair;
            ushort[] child1 = new ushort[length];
			ushort[] child2 = new ushort[length];
            int corssOverPoint = rand.Next(1, length);
            int i = 0;

            for (; i < corssOverPoint; i++)
            {
                child1[i] = this.val[i];
                child2[i] = p.val[i];
            }
            for(; i < length; i++)
            {
                child1[i] = p.val[i];
                child2[i] = this.val[i];
            }
            this.val = child1;
            p.val = child2;
        }
        public override IChromosome CreateNew()
        {
            return new TetrisChromosome(length);
        }
        //public int RowsCleared
        //{
        //    get;
        //    set;
        //}
        //public static double 

        /// <summary>
        /// Clone the chromosome
        /// </summary>
        public override IChromosome Clone()
        {
            return new TetrisChromosome(this);
        }
        public int calculateFitnessValue()
        {
            
            return val.Sum(x => x);
        }

        public override bool hasTheSameGens(IChromosome ch)
        {
            TetrisChromosome t = (TetrisChromosome) ch;
            for (int i = 0; i < val.Length; i++)
            {
                if (val[i] != t.val[i])
                    return false;
            }
            return true;
        }



    }
      

}
